<extend name="Public/base"/>
<block name="style">
<style type="text/css">
	.div-right{
		float:right;
	}
</style>
</block>
<block name="body">
	<div class="main-title">
		<h2>购物车</h2>
	</div>
	<form action="{:U('Order/addOrder')}" method="post" class="form-horizontal">
		<div class="form-item">
            <label class="">客户：</span></label>
            <select id="customers" name="customers_id">
                <option value="">请选择客户</option>
                <volist name="customerlist" id="ul">
                    <option value="{$ul.id}">{$ul.name}</option>
                </volist>
            </select>
            <input type="hidden" name="customers_name" id="customers_name">
            <input type="text" class="text input-large" name="cnametxt" id="cnametxt">
            <a class="btn" id="khcx" href="#">查 询</a>
        </div>
        <div class="form-item">
			<label class="">备注：</label>
			<input type="text" class="text input-600" name="remark" id="remark" maxlength="35" placeholder="备注限制35个字以内">
		</div>
		<div class="data-table table-striped">
		    <table class="">
		    	<thead>
		    		<tr>
		    			<th class="">商品分类</th>	
		    			<th class="">商品名称</th>
		    			<th class="">商品规格</th>
		    			<th class="">单位</th>
		    			<th class="">单价</th>
		    			<th class="">数量</th>
		    			<th class="">合计</th>
		    			<th class="">操作</th>
		    		</tr>
		    	</thead>
		    	<tbody>
		    		<notempty name="cart['goods_list']">
		    		<volist name="cart.goods_list" id="vo">
		    		<tr>
		    			<td><input type="hidden" id="typeid" name="typeid" value="{$vo.type_id}">{$vo.type_name}</td>
		    			<td>{$vo.goods_name}</td>
		    			<td><input type="hidden" id="speid" name="speid" value="{$vo.spe_id}">{$vo.spe_name}</td>
		    			<td>{$vo.unit}</td>
		    			<td><input type="text" class="price" name="price" id="price" value="{$vo.price}" readonly="readonly"></td>
		    			<td><input type="text" id="num" name="num" class="num" value="{$vo.num}"></td>
		    			<td><input type="text" name="total" id="total" value="{$vo.total}" readonly="readonly"></td>
		    			<td>
		    			<a href="{:U('Order/delCart?id='.$vo['type_id'].$vo['spe_id'])}" class="confirm ajax-get">删除</a></td>
		    		</tr>
		    		</volist>
		    		<else/>
		    		<td colspan="8" class="text-center">暂时还没有数据！</td>
		    		</notempty>
		    	</tbody>
		    </table>
		    <div class="page">
		    	{$_page}
		    </div>
	    </div>
	    <div class="form-item div-right">
	        <label class="item-label" id="alltotal">总价：{$cart.total_price} 元</label>
	    </div>
	    <div class="form-item">
	        <button class="btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button>
	        <a class="btn" href="{:U('Order/placeorder')}">返 回</a>
	    </div>
    </form>
</block>

<block name="script">
	<script type="text/javascript">
	$(function(){
		var dqalltotal = {$cart.total_price};

		$("#customers").change(function(){
			$("#customers_name").val($(this).find("option:selected").text());
		});

		$(".num",this).blur(function(){
            var price = $(this).parents("tr").find("#price").val();
    		var num = $(this).parents("tr").find("#num").val();
    		var speid = $(this).parents("tr").find("#speid").val();
    		var typeid = $(this).parents("tr").find("#typeid").val();
    		var oldtotal = $(this).parents("tr").find("#total").val();
            if(num != "")
    		{
    			$.post("{:U('Order/editCartNum')}",{typeid:typeid,id:speid,num:num},function(data){
    				var obj = JSON.parse(data);
    				if(obj){

    				}
    			})
    			var total = totalprice(price,num);
    			var cj = (total*100 - oldtotal*100)/100;
    			dqalltotal = (dqalltotal*100 + cj*100)/100;
    			$("#alltotal").text("总价：" + dqalltotal.toFixed(2) + " 元");
            	$(this).parents("tr").find("#total").val(total);
    		}
        });

        $("#khcx").click(function(){
        	var cnametxt = $("#cnametxt").val();
        	$.post("{:U('Order/getCustomerListByName')}",{cname:cnametxt},function(data){
        		$("#customers").empty();
        		for(var i=0;i<data.length;i++){
        			var id = data[i]["id"];
        			var name = data[i]["name"];
        			$("#customers").append("<option value="+data[i]["id"]+">"+data[i]["name"]+"</option>");
        		}
        		$("#customers_name").val($("#customers").find("option:selected").text());
        	})
        });
	});

	//合计金额
    function totalprice(price,num){
        var totalvar = price * num;
        return totalvar.toFixed(2);
    };
    //导航高亮
    highlight_subnav('{:U('Order/placeorder')}');
	</script>
</block>